# 需求1：爬取天涯首页的标题
# 需求2：使用代理IP

from urllib import request

url = 'https://bbs.tianya.cn/api?method=bbs.ice.getHotArticleList&params.pageSize=40&params.pageNum=1&var=apiData&_r=0.5536143377248306&_=162168845485'

# 1. 请求头部信息
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62',
    'Referer':'https://bbs.tianya.cn/'
}

# 2. 增加头部信息，构建完整的请求
res = request.Request(url, headers=headers)

# 4. 构建代理ip
handels = request.ProxyHandler({'http':'120.199.210.18:8383'})
opener = request.build_opener(handels)

# 3. 访问网页
file = opener.open(res)
print(file.read().decode('utf-8'))

# 3. 访问网页
#file = request.urlopen(res)
#print(file.read(1024).decode('utf-8'))
